var e = require("../@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", { value: !0 }),
  (exports.sensorManager = exports.default = void 0);
var a = require("../@babel/runtime/helpers/regeneratorRuntime"),
  t = require("../@babel/runtime/helpers/asyncToGenerator"),
  o = require("../@babel/runtime/helpers/classCallCheck"),
  n = require("../@babel/runtime/helpers/createClass"),
  i = require("../@babel/runtime/helpers/get"),
  r = require("../@babel/runtime/helpers/getPrototypeOf"),
  s = require("../@babel/runtime/helpers/inherits"),
  l = require("../@babel/runtime/helpers/createSuper"),
  d = e(require("./common/EventDispatcher")),
  u = e(require("./MapDataLoader/MapDataLoaderEvent")),
  p = e(require("./Location/Location")),
  c = e(require("./PathLoader/PathLoader")),
  f = e(require("./Location/BeaconStatus")),
  h = e(require("./MapDataLoader/OpMapDataLoader")),
  v = e(require("./MapDataLoader/BaseMapDataLoader")),
  g = e(require("./common/DeepClone")),
  _ = e(require("./Location/LocationHelper")),
  m = new (e(require("./SensorManager/SensorManager")).default)();
exports.sensorManager = m;
var L = (function (e) {
  s(y, e);
  var d,
    L = l(y);
  function y() {
    var e;
    return o(this, y), (e = L.call(this))._defineEvents(), e;
  }
  return (
    n(y, [
      {
        key: "create",
        value: function (e) {
          for (
            var a = e.apiHost,
              t = e.key,
              o = void 0 === t ? y.key : t,
              n = e.onlineMode,
              i = e.dataVersion,
              r = e.aoaBroker,
              s = void 0,
              l = void 0,
              d = 0,
              u = ["projectCode", "appCode"];
            d < u.length;
            d++
          ) {
            var f = u[d];
            if (e[f]) {
              (s = e[f]), (l = f);
              break;
            }
          }
          var g = void 0;
          if (
            (s || (g = "no appCode or projectCode"),
            a || (g = "no apiHost"),
            o || (g = "no key"),
            g)
          )
            console.error(g);
          else if (
            o ===
            wx.arrayBufferToBase64(
              new Uint8Array(
                (a + s).split("").map(function (e, a) {
                  return (e.charCodeAt(0) + a) % 255;
                }),
              ),
            )
          ) {
            (this._data = {
              accessCode: s,
              accessMode: l,
              apiHost: a,
              onlineMode: n,
              dataVersion: i,
              aoaBroker: r,
            }),
              (this._started = !1),
              (this._destroyed = !1);
            var L = _.default.getEngine();
            (this._floorMapper = new L.cm.fm()),
              (this._locationInstance = new p.default({
                floorMapper: this._floorMapper,
                sensorManager: m,
              })),
              (this._sensorManager = m);
            var b = h.default;
            if (("appCode" === l && (b = v.default), e.useSingletonLoader)) {
              var M = b.getInstance(this._data);
              M || ((M = new b(this._data)), (v.default.cacheMapLoader = M)),
                (this._mapDataLoader = M);
            } else this._mapDataLoader = new b(this._data);
            (this._walkPathLoader = new c.default(this._floorMapper)),
              this._initMapDataLoaderEvent("on"),
              this._initEngineEvents("on");
          } else console.error("key invalid");
        },
      },
      {
        key: "on",
        value: function (e, a) {
          i(r(y.prototype), "on", this).call(this, e, a),
            "ready" === e &&
              this.mapDataLoader.isAllMapLoadReady() &&
              a({ mapObjects: this.mapDataLoader.getAllMapObjects() });
        },
      },
      {
        key: "destroy",
        value: function () {
          this.stopLocationService(),
            (this._destroyed = !0),
            i(r(y.prototype), "dispose", this).call(this),
            this._initEngineEvents("off"),
            this._initMapDataLoaderEvent("off"),
            this._locationInstance.dispose(),
            this.emit("destroy");
        },
      },
      {
        key: "startLocationService",
        value: function () {
          this._destroyed ||
            this._started ||
            ((this._started = !0), this._locationInstance.start());
        },
      },
      {
        key: "stopLocationService",
        value: function () {
          this._destroyed ||
            (this._started &&
              ((this._started = !1), this._locationInstance.stop()));
        },
      },
      {
        key: "onAppShow",
        value: function () {
          this._started &&
            (console.info("app show, start loc"),
            this._locationInstance.start());
        },
      },
      {
        key: "onAppHide",
        value: function () {
          this._started &&
            (console.info("app hide, stop loc"), this._locationInstance.stop());
        },
      },
      {
        key: "_initEngineEvents",
        value: function (e) {
          var a = this._locationInstance,
            t = this._locationEvents;
          for (var o in t) {
            var n = t[o];
            a[e](o, n);
          }
        },
      },
      {
        key: "_defineEvents",
        value: function () {
          var e = this;
          (this._events = {
            allDataLoaded: function (a) {
              var t = a.mapObjects;
              t.forEach(function (a) {
                !(function (a) {
                  var t = a.buildingCode,
                    o = a.mapData,
                    n = o.mapinfo;
                  e._floorMapper.loadMapping((0, g.default)(a.floorInfos), t);
                  var i = {
                    buildingCode: t,
                    isBuilding: a.isBuilding,
                    aps: (0, g.default)(o.aps),
                    connection: (0, g.default)(o.connection),
                    roads: (0, g.default)(o.roads),
                    locateOptions: o.locateOptions || o.transRegion,
                    locateOptionsInGraphLayers: o.locateOptionsInGraphLayers,
                    navigationSettings: o.navigationSettings,
                    mapinfo: n,
                    floorInfosAtZ: a.floorInfosAtZ,
                    floorInfosAtRegion: a.floorInfosAtRegion,
                    floorBoundaries: a.floorBoundaries,
                    fpCoordTransform: a.fpCoordTransform,
                    isGroundMap: a.isGroundMap,
                  };
                  a.fpCoordTransform &&
                    a.buildingConfig.urls &&
                    a.buildingConfig.urls.fp &&
                    (i.fpUrl = a.buildingConfig.urls.fp),
                    e._locationInstance.loadBuildingInfo(i),
                    e._walkPathLoader.loadData(
                      o.connection,
                      o.roads,
                      t,
                      a.floorInfos,
                      "walk",
                    );
                })(a);
              });
              var o = {},
                n = e._walkPathLoader.pathInfo;
              n && (o.walkingHmmInfo = n),
                e._locationInstance.initHmm(o),
                e._locationInstance.setWalkingMode(),
                e.emit("ready", { mapObjects: t });
            },
          }),
            (this._locationEvents = {
              positionChanged: function (a) {
                var t = a.value;
                t.buildingCode && void 0 !== t.z
                  ? ((t.mapCode = t.buildingCode), e.emit("positionChanged", a))
                  : wx.showToast({
                      title: "b:" + t.buildingCode + ",z:" + t.z,
                      icon: "none",
                    });
              },
              orientationChanged: function (a) {
                var t = a.value.heading;
                e.emit("headingChanged", { value: t });
              },
              beaconStatusChanged: function (a) {
                var t = a.value;
                e.emit("bluetoothStatusChanged", {
                  value: t === f.default.OPENED,
                });
              },
            });
        },
      },
      {
        key: "_initMapDataLoaderEvent",
        value: function (e) {
          var a = this._mapDataLoader;
          "on" === e &&
            a.isAllMapLoadReady() &&
            this._events.allDataLoaded({ mapObjects: a.getAllMapObjects() }),
            a[e](u.default.ALL_DATA_LOADED, this._events.allDataLoaded);
        },
      },
      {
        key: "sensorManager",
        get: function () {
          return this._sensorManager;
        },
      },
      {
        key: "mapDataLoader",
        get: function () {
          return this._mapDataLoader;
        },
      },
      {
        key: "location",
        get: function () {
          return this._locationInstance;
        },
      },
      {
        key: "getPosition",
        value: function () {
          return this.location.getLastPosition();
        },
      },
      {
        key: "loadMap",
        value:
          ((d = t(
            a().mark(function e(t) {
              return a().wrap(
                function (e) {
                  for (;;)
                    switch ((e.prev = e.next)) {
                      case 0:
                        if (!this._data.aoaBroker) {
                          e.next = 3;
                          break;
                        }
                        return (
                          (e.next = 3),
                          this.sensorManager.aoaLocator.init(this._data)
                        );
                      case 3:
                        return e.abrupt(
                          "return",
                          this.mapDataLoader.loadMap(t),
                        );
                      case 4:
                      case "end":
                        return e.stop();
                    }
                },
                e,
                this,
              );
            }),
          )),
          function (e) {
            return d.apply(this, arguments);
          }),
      },
    ]),
    y
  );
})(d.default);
L.key = void 0;
var y = L;
exports.default = y;
